const axios = require("axios");
const codegen = require("../generators/codegen");
const fs = require("fs");
const ejs = require("ejs");
// Const template = require("art-template");
const path = require("path");

// Template.defaults.minimize = true;

const props = {
  swaggerUrl: "http://10.1.13.58:8888/online/swagger-ui.html",
  // swaggerUrl: "http://192.168.137.224:8889/swagger-ui.html",
  // swaggerUrl: "http://10.1.13.58:8889/swagger-ui.html",
  outPutFile: "index.d.ts"
};

const swaggerUrl = props.swaggerUrl.replace("swagger-ui.html", "v2/api-docs");
const outPutFile = props.outPutFile;
axios.get(swaggerUrl).then(res => {
  if (res.status === 200) {
    const swagger = res.data;
    const swaggerData = codegen.getViewForSwagger(swagger);
    // CopyTpl("../generators/app/templates/d.ts.ejs", outPutFile, swaggerData);
    copyTpl("../generators/app/templates/d.ts.ejs", outPutFile, swaggerData);
  }
});

const copyTpl = (templatePath, outputFile, data) => {
  const pth = path.resolve(__dirname, templatePath);
  fs.writeFileSync("./index.json", JSON.stringify(data), { encoding: "utf-8" });
  ejs.renderFile(pth, data, {}, function(err, str) {
    // Str => 输出渲染后的 HTML 字符串
    fs.writeFileSync(outputFile, str, { encoding: "utf-8" });
  });
  // Const content = template(pth, data, {});
  // fs.writeFileSync(outputFile, content, { encoding: "utf-8" });
};
